package cn.shyouchi.wuguanjia.android.ui.statistic;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.attendance.AttendanceApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class StatisticGroupIntegratedActivity extends BaseActivity {


    AttendanceApi mAttendanceApi;


    String date;
    boolean isYear;
    String projectid;
    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_statistic_list)
    RecyclerView mRvStatisticList;



    StatisticGroupIntegratedAdapter mStatisticGroupIntegratedAdapter;
    List<UserMonthRank> mStatisticGroupEntities = new ArrayList<>();

    @Override
    public int initContentView() {
        return R.layout.activity_statistic_group_integrated;
    }



    @Override
    public void initInjector() {
        mAttendanceApi = new AttendanceApi(mContext);
        date = getIntent().getStringExtra("date");
        isYear = getIntent().getBooleanExtra("year", false);
        projectid = getIntent().getStringExtra("projectid");
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());

        mStatisticGroupIntegratedAdapter = new StatisticGroupIntegratedAdapter(mStatisticGroupEntities);

        mRvStatisticList.setAdapter(mStatisticGroupIntegratedAdapter);
        mRvStatisticList.setLayoutManager(new LinearLayoutManager(mContext));



        fetchMothRankData();
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    private void fetchMothRankData() {
        mAttendanceApi.getIntegratedStatisticData(date, isYear, projectid)
                .subscribeOn(Schedulers.io())
                .map(new Func1<BaseRequestResult<List<UserMonthRank>>, List<UserMonthRank>>() {
                    @Override
                    public List<UserMonthRank> call(BaseRequestResult<List<UserMonthRank>> result) {
                            for (UserMonthRank r : result.getResultMessage()) {
                                r.setType(StatisticGroupAdapter.STATISTIC_INTEGRATED);
                            }
                        return result.getResultMessage();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<List<UserMonthRank>>(mContext) {
                    @Override
                    public void onNext(List<UserMonthRank> userMonthRanks) {
                        mStatisticGroupEntities.clear();
                        mStatisticGroupEntities.addAll(userMonthRanks);
                        mStatisticGroupIntegratedAdapter.notifyDataSetChanged();
                    }
                });
    }

}
